<template>
  <view class="page">
    <!-- 顶部车辆卡片 -->
    <view class="car-card">
      <image class="car-img" :src="carInfo.image" mode="aspectFill"></image>
      <view class="car-text">
        <view>{{ carInfo.name }}</view>
        <view class="sub">{{ carInfo.price }}</view>
        <view class="vin">{{ carInfo.plate }}</view>
      </view>
      <view class="tag" :class="carInfo.type">{{ carInfo.typeText }}</view>
    </view>

    <!-- 文件清单 -->
    <view class="section">
      <view class="section-title">文件清单</view>
      <view class="form-card readonly">
        <view class="cell" v-for="(item, index) in fileList" :key="index">
          <view class="label">{{ item.label }}</view>
          <view class="value-right">
            <text class="status-text">{{ item.url ? '已上传' : '—' }}</text>
            <image
              v-if="item.url"
              class="thumb"
              :src="item.url"
              mode="aspectFill"
              @tap.stop="previewPic(item.url)"
            />
            <text class="ic">👁</text>
          </view>
        </view>
      </view>
    </view>

    <!-- 经办记录 -->
    <view class="section">
      <view class="section-title">经办记录</view>
      <view class="timeline">
        <view class="t-row t-hd">
          <text class="t-col">节点</text>
          <text class="t-col">经办人/岗位</text>
          <text class="t-col">时间</text>
          <text class="t-col">结果</text>
        </view>
        <view class="t-row" v-for="(item, index) in taskList" :key="index">
          <text class="t-col">{{ item.stepName }}</text>
          <text class="t-col">{{ item.userName }}<br/>{{ item.position }}</text>
          <text class="t-col">{{ item.time }}</text>
          <text class="t-col">{{ item.result }}</text>
        </view>
      </view>
    </view>

    <!-- 意见栏 -->
    <view class="section">
      <view class="section-title">意见栏</view>
      <view class="op-box">
        <textarea v-model="opinion" placeholder="填写审核意见（可选）" maxlength="300" auto-height />
        <view class="hint">当前节点：<text class="danger">材料审核</text></view>
      </view>
    </view>

    <!-- 底部操作按钮 -->
    <view class="bottom-btn-box">
      <button class="btn btn-gray" @tap="onReject">驳回</button>
      <button class="btn btn-primary" @tap="onApprove">同意</button>
    </view>
  </view>
</template>

<script>
import common_config from '@/static/js/common.js';
const config = common_config.config;
let userInfo;

export default {
  data() {
    return {
      // 车辆信息
      carInfo: {
        id: '',
        name: '',
        vin: '',
        plate: '',
        image: '',
        price: '',
        type: '',
        typeText: ''
      },

      // 文件清单
      fileList: [
        { label: '合同', url: 'https://kehu-1256834579.cos.ap-shanghai.myqcloud.com/yuanxiang/logo2.png' },
        { label: '合同附件', url: 'https://kehu-1256834579.cos.ap-shanghai.myqcloud.com/yuanxiang/logo2.png' },
        { label: '登记证', url: 'https://kehu-1256834579.cos.ap-shanghai.myqcloud.com/yuanxiang/logo2.png' },
        { label: '行驶证', url: 'https://kehu-1256834579.cos.ap-shanghai.myqcloud.com/yuanxiang/logo2.png' },
        { label: '发票', url: 'https://kehu-1256834579.cos.ap-shanghai.myqcloud.com/yuanxiang/logo2.png' },
        { label: '付款截图', url: 'https://kehu-1256834579.cos.ap-shanghai.myqcloud.com/yuanxiang/logo2.png' }
      ],

      // 经办记录
      taskList: [
        {
          stepName: '资料申报',
          userName: '王琳琳',
          position: '业务部',
          time: '2025年6月12日 16:39:24',
          result: '提交'
        },
        {
          stepName: '材料审核',
          userName: '陈钊',
          position: '二手车业务部',
          time: '2025年6月12日 16:39:24',
          result: '同意'
        },
        {
          stepName: '材料审核',
          userName: '陈栋',
          position: '安德部',
          time: '2025年6月12日 16:39:24',
          result: '同意'
        }
      ],

      // 审核意见
      opinion: ''
    }
  },

  onLoad(options) {
    // 获取传递过来的车辆信息
    this.carInfo.id = options.id || '';
    this.carInfo.name = decodeURIComponent(options.car_name || '');
    this.carInfo.vin = decodeURIComponent(options.vin || '');
    this.carInfo.plate = decodeURIComponent(options.plate || '');
    this.carInfo.image = decodeURIComponent(options.image || 'https://kehu-1256834579.cos.ap-shanghai.myqcloud.com/yuanxiang/logo2.png');
    this.carInfo.price = '新车指导价 38.88万元';
    this.carInfo.type = options.type || 'sale';
    this.carInfo.typeText = this.carInfo.type === 'purchase' ? '采购' : '销售';

    // 获取用户信息
    common_config.common_if_login(() => {}, () => {
      userInfo = uni.getStorageSync('userInfo');
      try { userInfo = JSON.parse(userInfo); } catch(e) {}
      
      // 加载数据
      // this.loadData();
    });
  },

  methods: {
    // 预览图片
    previewPic(url) {
      if (url) {
        uni.previewImage({
          urls: [url],
          current: 0
        });
      }
    },

    // 驳回
    onReject() {
      uni.showModal({
        title: '确认驳回',
        content: '确定要驳回该申请吗？',
        success: (res) => {
          if (res.confirm) {
            this.submitReview(2); // 2=驳回
          }
        }
      });
    },

    // 同意
    onApprove() {
      uni.showModal({
        title: '确认同意',
        content: '确定要通过该申请吗？',
        success: (res) => {
          if (res.confirm) {
            this.submitReview(1); // 1=同意
          }
        }
      });
    },

    // 提交审核
    submitReview(status) {
      uni.showLoading({ title: status === 1 ? '提交中' : '驳回中' });
      
      /**
       * 接口说明：
       * 接口地址：config.request_url + '/index/business/material_review?token=' + userInfo.token
       * 请求方式：POST
       * 请求参数：
       * {
       *   id: this.carInfo.id,     // 业务ID
       *   status: status,           // 审核状态：1=同意, 2=驳回
       *   opinion: this.opinion     // 审核意见
       * }
       * 
       * 返回数据格式：
       * {
       *   code: 200,
       *   msg: '操作成功'
       * }
       */
      
      uni.request({
        url: config.request_url + '/index/business/material_review?token=' + (userInfo?.token || ''),
        method: 'POST',
        header: { 'Content-Type': 'application/x-www-form-urlencoded' },
        data: {
          id: this.carInfo.id,
          status: status,
          opinion: this.opinion
        },
        success: (res) => {
          uni.hideLoading();
          const r = res.data || {};
          if (r.code == 200) {
            uni.showToast({
              title: status === 1 ? '审核通过' : '已驳回',
              icon: 'success'
            });
            
            setTimeout(() => {
              uni.navigateBack();
            }, 1500);
          } else {
            common_config.calert(r.msg || '操作失败');
          }
        },
        fail: () => {
          uni.hideLoading();
          common_config.calert('网络异常');
        }
      });
    }
  }
}
</script>

<style>
/* 页面与卡片 */
.page {
  background: #f6f8ff;
  min-height: 100vh;
  padding: 32rpx 32rpx 160rpx;
}

.section {
  background: #fff;
  border-radius: 20rpx;
  padding: 24rpx;
  margin-bottom: 28rpx;
  box-shadow: 0 4rpx 12rpx rgba(0,0,0,0.04);
}

.section-title {
  font-size: 30rpx;
  font-weight: 600;
  color: #333;
  margin-bottom: 12rpx;
}

.car-card {
  background: #fff;
  border-radius: 20rpx;
  padding: 20rpx;
  display: flex;
  gap: 20rpx;
  align-items: center;
  margin-bottom: 28rpx;
  box-shadow: 0 4rpx 12rpx rgba(0,0,0,0.05);
}

.car-img {
  width: 220rpx;
  height: 130rpx;
  border-radius: 12rpx;
  flex-shrink: 0;
}

.car-text {
  flex: 1;
  font-size: 28rpx;
  color: #333;
  line-height: 1.6;
}

.car-text .sub {
  color: #777;
  font-size: 26rpx;
}

.vin {
  font-size: 26rpx;
  color: #888;
  margin-top: 4rpx;
}

/* 类型标签 - 右下角 */
.car-card {
  position: relative;
}

.car-card .tag {
  position: absolute;
  bottom: 20rpx;
  right: 20rpx;
  font-size: 24rpx;
  color: #fff;
  border-radius: 6rpx;
  padding: 4rpx 12rpx;
  white-space: nowrap;
}

.car-card .tag.purchase {
  background-color: #FF9500;
}

.car-card .tag.sale {
  background-color: #FF3B30;
}

/* 表单表格 */
.form-card {
  background: #fff;
  border: 1rpx solid #e8eef6;
  border-radius: 16rpx;
  overflow: hidden;
}

.cell {
  display: flex;
  align-items: center;
  padding: 22rpx 18rpx;
  border-bottom: 1rpx solid #edf2f7;
}

.cell:last-child {
  border-bottom: none;
}

.label {
  width: 180rpx;
  font-size: 28rpx;
  color: #475569;
}

.value-right {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: flex-end;
}

.status-text {
  font-size: 28rpx;
  color: #64748b;
  margin-right: 12rpx;
}

.thumb {
  width: 72rpx;
  height: 72rpx;
  border-radius: 8rpx;
  margin-right: 12rpx;
}

.ic {
  width: 44rpx;
  text-align: center;
  font-size: 30rpx;
  color: #9aa5b1;
}

/* 经办记录 */
.timeline {
  border: 1rpx solid #e8eef6;
  border-radius: 12rpx;
  overflow: hidden;
}

.t-row {
  display: flex;
  border-bottom: 1rpx solid #edf2f7;
}

.t-row:last-child {
  border-bottom: none;
}

.t-hd {
  background: #f8fafc;
  font-weight: 600;
  color: #475569;
}

.t-col {
  flex: 1;
  padding: 20rpx 12rpx;
  font-size: 26rpx;
  text-align: center;
  word-break: break-all;
}

.t-col:not(:last-child) {
  border-right: 1rpx solid #edf2f7;
}

/* 意见栏 */
.op-box {
  padding: 0;
}

.op-box textarea {
  width: 100%;
  min-height: 200rpx;
  border: 1rpx solid #e8eef6;
  border-radius: 12rpx;
  padding: 20rpx;
  font-size: 28rpx;
  box-sizing: border-box;
}

.hint {
  margin-top: 16rpx;
  font-size: 26rpx;
  color: #64748b;
}

.danger {
  color: #ef4444;
  font-weight: 600;
}

/* 底部按钮 */
.bottom-btn-box {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  background: #fff;
  padding: 18rpx 28rpx;
  box-shadow: 0 -4rpx 10rpx rgba(0,0,0,0.06);
  display: flex;
  gap: 20rpx;
}

.btn {
  flex: 1;
  height: 90rpx;
  line-height: 90rpx;
  font-size: 30rpx;
  border-radius: 45rpx;
  font-weight: 600;
  border: none;
}

.btn-gray {
  background: #e5e7eb;
  color: #6b7280;
}

.btn-primary {
  background: #007aff;
  color: #fff;
}

.btn:active {
  opacity: .9;
}
</style>

